﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CollBrowser
{
    /// <summary>
    /// 原素搜索类
    /// </summary>
    public class ElementSearch
    {

        private System.Collections.Generic.IEnumerable<PathItem> pathImtes = new List<PathItem>();

        /// <summary>
        /// 要搜索的路径表达式
        /// </summary>
        public System.Collections.Generic.IEnumerable<PathItem> PathItems
        {
            get { return pathImtes; }
            set { pathImtes = value; }
        }

        /// <summary>
        /// 当前选择的原素
        /// </summary>
        private System.Windows.Forms.HtmlElement currentEl = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="pathImtes">要搜索的路径表达式</param>
        public ElementSearch(System.Collections.Generic.IEnumerable<PathItem> pathImtes)
        {
            this.pathImtes = pathImtes;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ElementSearch()
        {

        }


        /// <summary>
        /// 找指定路径的原素
        /// </summary>
        /// <param name="dom">文档对像</param>
        /// <returns></returns>
        public System.Windows.Forms.HtmlElement Find(System.Windows.Forms.HtmlDocument dom)
        {
            return FindElement(dom);
        }

        #region 子类可以见到的方法
        /// <summary>
        /// 找到指定路径的 原素 ,同有找到返回null
        /// </summary>
        /// <param name="dom">web 浏览器对像加载完成后的文档对像</param>
        /// <returns>找到指定路径的 原素</returns>
        protected virtual System.Windows.Forms.HtmlElement FindElement(System.Windows.Forms.HtmlDocument dom )
        {
            //得到最后的一个ID
            string lastID = "";
            //当前进行到层次的索引
            int currentIndex = 0;

            //有ID才进行寻找
            if (pathImtes.Where(p => p.id.Length > 0).Count() > 0)
            {
                int sk = 0;
                foreach (var item in PathItems)
                {
                    sk++;
                    if (item.id != "")
                    {
                        lastID = item.id;
                        currentIndex = sk;
                    }
                }

                //最后一个原素表示就是他自己
                if (currentIndex >= pathImtes.Count() - 1)
                {
                    return dom.GetElementById(pathImtes.Last().id);
                }
                else
                { 
                    //需求重新查找
                    return FindElementByPathImtes(dom.GetElementById(lastID), currentIndex);
                }
            }
            else
            {
                var currentEl = FindElementByPathImtes(dom.GetElementsByTagName("html")[0], 0);

                return currentEl;
            }

            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="el">要查找的基原素</param>
        /// <param name="currentIndex">当前搜索的层次 PathItems 每一行表示HTML树中的一个层次</param>
        /// <returns></returns>
        protected System.Windows.Forms.HtmlElement FindElementByPathImtes(System.Windows.Forms.HtmlElement el, int currentIndex)
        {


            if (currentIndex != pathImtes.Count())
            {
                //最后的原素
                if (currentIndex == pathImtes.Count() - 1)
                {
                    currentEl =  FindElementByPathImte(el, pathImtes.ElementAt(currentIndex));
                }
                else
                {
                    currentEl = FindElementByPathImte(el, pathImtes.ElementAt(currentIndex));
                    if (currentEl == null)
                    {
                        throw new Exception("没有找到指定的原素路径深度" + currentIndex + "\r\n" + pathImtes.ElementAt(currentIndex).ToString());
                    }

                    currentIndex = 1 + currentIndex;
                    currentEl = FindElementByPathImtes(currentEl, currentIndex);
                }
            }


            return currentEl;
        }

        /// <summary>
        /// 找到指定的原素
        /// </summary>
        /// <param name="el">传入的基HTML原素</param>
        /// <param name="item">当前列</param>
        /// <returns>返回已经找到的HTML原素</returns>
        protected System.Windows.Forms.HtmlElement FindElementByPathImte(System.Windows.Forms.HtmlElement el, PathItem item)
        {
            if (item.e.ToUpper() != "HTML")
            {

                try
                {
                    return el.GetElementsByTagName(item.e)[int.Parse(item.index)];
                }
                catch {
                    return null;
                }
            }
            else
            {
                return el;
            }
        }
        #endregion

    }
}
